home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / mewin10s.zip / DISPLAY.C < prev    next >
C/C++ Source or Header  |  1992-03-08  |  44KB  |  1,946 lines

  1. /*
  2.  * The functions in this file handle redisplay. There are two halves, the
  3.  * ones that update the virtual display screen, and the ones that make the
  4.  * physical display screen the same as the virtual display screen. These
  5.  * functions use hints that are left in the windows by the commands.
  6.  *
  7.  */
  8.  
  9. #include    <stdio.h>
  10. #include    "estruct.h"
  11. #include    "eproto.h"
  12. #include    "edef.h"
  13. #include    "elang.h"
  14.  
  15. static VIDEO   **vscreen;               /* Virtual screen. */
  16. #if    MEMMAP == 0
  17. static VIDEO   **pscreen;               /* Physical screen. */
  18. #endif
  19.  
  20. #if     WINDOW_MSWIN
  21. static  int     foulcursor = FALSE;     /* see vtscreen() & movecursor() */
  22. #endif
  23.  
  24. #if     WINDOW_MSWIN
  25. #define MROW    (term.t_nrow)
  26. #define MCOL    (term.t_ncol)
  27. #else
  28. #define MROW    (term.t_mrow)
  29. #defien MCOL    (term.t_mcol)
  30. #endif
  31.  
  32. /*    some local function declarations    */
  33.  
  34. #if    PROTO
  35. #if    MEMMAP
  36. extern PASCAL NEAR updateline(int row, struct VIDEO *vp1);
  37. #else
  38. extern PASCAL NEAR updateline(int row, struct VIDEO *vp1, struct VIDEO *vp2);
  39. #endif
  40. #else
  41. extern PASCAL NEAR updateline();
  42. #endif
  43.  
  44. /*
  45.  * Initialize the data structures used by the display code. The edge vectors
  46.  * used to access the screens are set up. The operating system's terminal I/O
  47.  * channel is set up. All the other things get initialized at compile time.
  48.  * The original window has "WFCHG" set, so that it will get completely
  49.  * redrawn on the first call to "update".
  50.  */
  51.  
  52. PASCAL NEAR vtinit()
  53. {
  54.     register int i;
  55.     register VIDEO *vp;
  56.     int     result;
  57.  
  58.     TTopen();        /* open the screen */
  59.     TTkopen();        /* open the keyboard */
  60.     TTrev(FALSE);
  61.  
  62.     /* allocate the virtual screen pointer array */
  63.     vscreen = (VIDEO **) malloc(MROW*sizeof(VIDEO *));
  64.     if (vscreen == NULL)
  65. #if     WINDOW_MSWIN
  66.                 return FALSE;
  67. #else
  68.         meexit(1);
  69. #endif
  70.  
  71. #if    MEMMAP == 0
  72.     /* allocate the physical shadow screen array */
  73.     pscreen = (VIDEO **) malloc(MROW*sizeof(VIDEO *));
  74.     if (pscreen == NULL)
  75. #if     WINDOW_MSWIN
  76.                 return FALSE;
  77. #else
  78.         meexit(1);
  79. #endif
  80. #endif
  81.  
  82.         result = TRUE;
  83.     /* for every line in the display */
  84.     for (i = 0; i < MROW; ++i) {
  85.  
  86.         /* allocate a virtual screen line */
  87.         vp = (VIDEO *) malloc(sizeof(VIDEO)+MCOL);
  88.         if (vp == NULL) {
  89. #if     WINDOW_MSWIN
  90.                         result = FALSE;
  91. #else
  92.                 meexit(1);
  93. #endif
  94.         }
  95.         else {
  96.             vp->v_flag = 0;        /* init change flags */
  97. #if    COLOR
  98.             vp->v_rfcolor = 7;    /* init fore/background colors */
  99.             vp->v_rbcolor = 0;
  100. #endif
  101.         }
  102.         /* connect virtual line to line array */
  103.         vscreen[i] = vp;
  104.  
  105. #if    MEMMAP == 0
  106.         /* allocate and initialize physical shadow screen line */
  107.         vp = (VIDEO *) malloc(sizeof(VIDEO)+MCOL);
  108.         if (vp == NULL) {
  109. #if     WINDOW_MSWIN
  110.                         result = FALSE;
  111. #else
  112.                 meexit(1);
  113. #endif
  114.         }
  115.         else {
  116.             vp->v_flag = VFNEW;
  117. #if    INSDEL
  118.             vp->v_rline = i;    /* set requested line position */
  119. #endif
  120.         }
  121.         pscreen[i] = vp;
  122. #endif
  123.     }
  124.     return result;;
  125. }
  126.  
  127. #if    CLEAN || WINDOW_MSWIN
  128. /* free up all the dynamically allocated video structures */
  129.  
  130. PASCAL NEAR vtfree()
  131. {
  132.     int i;
  133.     for (i = 0; i < MROW; ++i) {
  134.         if (vscreen && vscreen[i]) free(vscreen[i]);
  135. #if    MEMMAP == 0
  136.         if (pscreen && pscreen[i]) free(pscreen[i]);
  137. #endif
  138.     }
  139.     if (vscreen) free(vscreen);
  140. #if    MEMMAP == 0
  141.     if (pscreen) free(pscreen);
  142. #endif
  143. }
  144. #endif
  145.  
  146. #if     WINDOW_MSWIN
  147. /* vtscreen:    map a screen into the Virtual Terminal system */
  148. /* ========                                                   */
  149.  
  150. PASCAL NEAR vtscreen (SCREEN *sp)
  151. {
  152.     TTflush();
  153.     term.t_roworg = sp->s_roworg;
  154.     term.t_colorg = sp->s_colorg;
  155.     term.t_nrow = sp->s_nrow;
  156.     term.t_ncol = sp->s_ncol;
  157.  
  158.     vscreen = sp->s_virtual;
  159.     pscreen = sp->s_physical;
  160.     term.t_selscr (sp);
  161.     foulcursor = TRUE;
  162. } /* vtscreen */
  163.  
  164. /* vtinitscr: build a virtual terminal resource for a new screen */
  165. /* =========                                                     */
  166.  
  167. int PASCAL NEAR vtinitscr (SCREEN *sp, int nrow, int ncol)
  168.  
  169. /* returns TRUE if successful */
  170. {
  171.     int     result;
  172.  
  173.     term.t_nrow = nrow;
  174.     term.t_ncol = ncol;
  175.     term.t_roworg = 0;
  176.     term.t_colorg = 0;
  177.     if ((result = vtinit ()) == TRUE) {
  178.         sp->s_virtual = vscreen;
  179.         sp->s_physical = pscreen;
  180.         sp->s_nrow = nrow;
  181.         sp->s_ncol = ncol;
  182.         sp->s_roworg = 0;
  183.         sp->s_colorg = 0;
  184.     }
  185.     else {
  186.         vtfree ();
  187.         sp->s_virtual = NULL;
  188.         sp->s_physical = NULL;
  189.     }
  190.     return result;
  191. } /* vtinitscr */
  192.  
  193. /* vtfreescr:  delete a virtual terminal resource for a dying screen */
  194. /* =========                                                         */
  195.  
  196. PASCAL NEAR vtfreescr (SCREEN *sp)
  197. {
  198.     vtscreen (sp);
  199.     vtfree ();
  200.     vtscreen (first_screen);    /* switch to an existing screen */
  201. } /* vtfreescr */
  202.  
  203. /* vtsizescr:   resize the virtual terminal resources */
  204. /* =========                                          */
  205.  
  206. int PASCAL NEAR vtsizescr (SCREEN *sp, int nrow, int ncol)
  207.  
  208. /* returns TRUE if successful. Otherwise, the old VIDEO structures are
  209.    preserved. */
  210. {
  211.     VIDEO   **oldvvp, **oldpvp;
  212.     int     oldnrow, oldncol;
  213.  
  214.     oldvvp = sp->s_virtual;
  215.     oldpvp = sp->s_physical;
  216.     oldnrow = sp->s_nrow;
  217.     oldncol = sp->s_ncol;
  218.     if (vtinitscr (sp, nrow, ncol) == TRUE) {
  219.         /* success! let's free the old VIDEO structures */
  220.         WINDOW  *wp;
  221.         
  222.         vscreen = oldvvp;
  223.         pscreen = oldpvp;
  224.         term.t_nrow = oldnrow;
  225.         term.t_ncol = oldncol;
  226.         vtfree ();      /* get rid of the old VIDEOs (kept up to now in
  227.                case the new allocation had failed) */
  228.         vtscreen (sp);  /* put the new VIDEOs into active duty */
  229.     for (wp = sp->s_first_window; wp != NULL; wp = wp->w_wndp) {
  230.         /* the VIDEOs have been wiped clean. Let's have every window
  231.            marked for a complete redraw */
  232.         wp->w_flag |= WFHARD|WFMODE;
  233.     }
  234.         term.t_sizscr (sp); /* resize the MDI window */
  235.         return TRUE;
  236.     }
  237.     else {
  238.         /* failure! we still need some kind of VIDEO structures, so we
  239.        reuse the old ones */
  240.     term.t_nrow = oldnrow;
  241.     term.t_ncol = oldncol;
  242.     sp->s_virtual = vscreen = oldvvp;
  243.     sp->s_physical = pscreen = oldpvp;
  244.         mlabort (TEXT94);   /* "out of memory" */
  245.         return FALSE;
  246.     }
  247. } /* vtsizescr */
  248. #endif
  249.  
  250. /*
  251.  * Clean up the virtual terminal system, in anticipation for a return to the
  252.  * operating system. Move down to the last line and clear it out (the next
  253.  * system prompt will be written in the line). Shut down the channel to the
  254.  * terminal.
  255.  */
  256. PASCAL NEAR vttidy()
  257. {
  258.     mlerase();
  259.     movecursor(term.t_nrow, 0);
  260.     TTflush();
  261.     TTclose();
  262.     TTkclose();
  263. }
  264.  
  265. /*
  266.  * Set the virtual cursor to the specified row and column on the virtual
  267.  * screen. There is no checking for nonsense values; this might be a good
  268.  * idea during the early stages.
  269.  */
  270. PASCAL NEAR vtmove(row, col)
  271.  
  272. int row, col;
  273.  
  274. {
  275.     vtrow = row;
  276.     vtcol = col;
  277. }
  278.  
  279. /* Write a character to the virtual screen. The virtual row and
  280.    column are updated. If we are not yet on left edge, don't print
  281.    it yet. If the line is too long put a "$" in the last column.
  282.    This routine only puts printing characters into the virtual
  283.    terminal buffers. Only column overflow is checked.
  284. */
  285.  
  286. PASCAL NEAR vtputc(c)
  287.  
  288. int c;
  289.  
  290. {
  291.     register VIDEO *vp;    /* ptr to line being updated */
  292.  
  293.     vp = vscreen[vtrow];
  294.  
  295.     if (c == '\t') {
  296.         do {
  297.             vtputc(' ');
  298.         } while (((vtcol + taboff) % (tabsize)) != 0);
  299.     } else if (vtcol >= term.t_ncol) {
  300.         ++vtcol;
  301.         vp->v_text[term.t_ncol - 1] = '$';
  302.     } else if (disphigh && c > 0x7f) {
  303.         vtputc('^');
  304.         vtputc('!');
  305.         c -= 0x80;
  306.         if (c == '\t') {
  307.             vtputc('^');
  308.             vtputc('I');
  309.         } else
  310.             vtputc(c);
  311.     } else if (c < 0x20 || c == 0x7F) {
  312.         vtputc('^');
  313.         vtputc(c ^ 0x40);
  314.     } else {
  315.         if (vtcol >= 0)
  316.             vp->v_text[vtcol] = c;
  317.         ++vtcol;
  318.     }
  319. }
  320.  
  321. /*
  322.  * Erase from the end of the software cursor to the end of the line on which
  323.  * the software cursor is located.
  324.  */
  325. PASCAL NEAR vteeol()
  326. {
  327.     register VIDEO    *vp;
  328.  
  329.     vp = vscreen[vtrow];
  330.     while (vtcol < term.t_ncol) {
  331.         if (vtcol >= 0)
  332.         vp->v_text[vtcol] = ' ';
  333.     vtcol++;
  334.     }
  335. }
  336.  
  337. /* upscreen:    user routine to force a screen update
  338.         always finishes complete update     */
  339.  
  340. PASCAL NEAR upscreen(f, n)
  341.  
  342. int f,n;    /* prefix flag and argument */
  343.  
  344. {
  345.     update(TRUE);
  346.     return(TRUE);
  347. }
  348.  
  349. /*
  350.  * Make sure that the display is right. This is a three part process. First,
  351.  * scan through all of the windows looking for dirty ones. Check the framing,
  352.  * and refresh the screen. Second, make sure that "currow" and "curcol" are
  353.  * correct for the current window. Third, make the virtual and physical
  354.  * screens the same.
  355.  */
  356. PASCAL NEAR update(force)
  357.  
  358. int force;    /* force update past type ahead? */
  359.  
  360. {
  361.     register WINDOW *wp;
  362. #if     WINDOW_MSWIN
  363.     SCREEN  *sp;
  364. #endif
  365. #if    TYPEAH
  366.     if (force == FALSE && typahead())
  367.         return(TRUE);
  368. #endif
  369. #if    VISMAC == 0
  370.     if (force == FALSE && kbdmode == PLAY)
  371.         return(TRUE);
  372. #endif
  373.  
  374. #if     WINDOW_MSWIN
  375.         if (force == FALSE) {
  376.             defferupdate = TRUE;
  377.             return TRUE;
  378.         }
  379.         else defferupdate = FALSE;
  380.  
  381.     /* loop through all screens to update even partially hidden ones.
  382.        Note that we process the "first" screen last */
  383.     sp = first_screen;
  384.     do {
  385.         sp = sp->s_next_screen;
  386.         if (sp == (SCREEN *)NULL) sp = first_screen;
  387.         vtscreen (sp);
  388.         wheadp = sp->s_first_window;
  389. #endif
  390.  
  391.     /* update any windows that need refreshing */
  392.     wp = wheadp;
  393.     while (wp != NULL) {
  394.         if (wp->w_flag) {
  395.             /* if the window has changed, service it */
  396.             reframe(wp);    /* check the framing */
  397.             if ((wp->w_flag & ~WFMODE) == WFEDIT)
  398.                 updone(wp);    /* update EDITed line */
  399.             else if (wp->w_flag & ~WFMOVE)
  400.                 updall(wp);    /* update all lines */
  401.             if (wp->w_flag & WFMODE)
  402.                 modeline(wp);    /* update modeline */
  403.             wp->w_flag = 0;
  404.             wp->w_force = 0;
  405.         }
  406.  
  407. #if    1    /* take this out before the release! */
  408.     if (wp->w_wndp == wheadp) {    /* erroneously circular list */
  409.         wp->w_wndp = (WINDOW *)NULL;
  410.         mlwrite("DAN!!! a bogus circular window list!!!");
  411.         TTgetc();
  412.     }
  413. #endif
  414.         /* on to the next window */
  415.         wp = wp->w_wndp;
  416.     }
  417.  
  418.     /* recalc the current hardware cursor location */
  419. #if     WINDOW_MSWIN
  420.     if (sp == first_screen) {
  421. #endif
  422.     updpos();
  423. #if     WINDOW_MSWIN
  424.     }
  425. #endif
  426.  
  427. #if    MEMMAP
  428.     /* update the cursor and flush the buffers */
  429.     movecursor(currow, curcol - lbound);
  430. #endif
  431.  
  432.     /* check for lines to de-extend */
  433.     upddex();
  434.  
  435.     /* if screen is garbage, re-plot it */
  436.     if (sgarbf != FALSE)
  437.         if (gflags & GFSDRAW)
  438.             sgarbf = FALSE;
  439.         else
  440.             updgar();
  441.  
  442.     /* update the virtual screen to the physical screen */
  443.     updupd(force);
  444. #if     WINDOW_MSWIN
  445.     } while (sp != first_screen);
  446.     sgarbf = FALSE;
  447. #endif
  448.  
  449.     /* update the cursor and flush the buffers */
  450.     movecursor(currow, curcol - lbound);
  451.     TTflush();
  452.  
  453.     return(TRUE);
  454. }
  455.  
  456. /*    reframe:    check to see if the cursor is on in the window
  457.             and re-frame it if needed or wanted        */
  458.  
  459. PASCAL NEAR reframe(wp)
  460.  
  461. WINDOW *wp;
  462.  
  463. {
  464.     register LINE *lp;    /* search pointer */
  465.     register LINE *rp;    /* reverse search pointer */
  466.     register LINE *hp;    /* ptr to header line in buffer */
  467.     register LINE *tp;    /* temp debugging pointer */
  468.     register int i;        /* general index/# lines to scroll */
  469.     register int nlines;    /* number of lines in current window */
  470.  
  471.     /* figure out our window size */
  472.     nlines = wp->w_ntrows;
  473.     if (modeflag == FALSE)
  474.         nlines++;
  475.  
  476.     /* if not a requested reframe, check for a needed one */
  477.     if ((wp->w_flag & WFFORCE) == 0) {
  478.         lp = wp->w_linep;
  479.         for (i = 0; i < nlines; i++) {
  480.  
  481.             /* if the line is in the window, no reframe */
  482.             if (lp == wp->w_dotp)
  483.                 return(TRUE);
  484.  
  485.             /* if we are at the end of the file, reframe */
  486.             if (lp == wp->w_bufp->b_linep)
  487.                 break;
  488.  
  489.             /* on to the next line */
  490.             lp = lforw(lp);
  491.         }
  492.     }
  493.  
  494.     /* reaching here, we need a window refresh */
  495.     i = wp->w_force;
  496.  
  497.     /* if smooth scrolling is enabled,
  498.         first.. have we gone off the top? */
  499.     if (sscroll && ((wp->w_flag & WFFORCE) == 0)) {
  500.         /* search thru the buffer looking for the point */
  501.         tp = lp = rp = wp->w_linep;
  502.         hp = wp->w_bufp->b_linep;
  503.         while ((lp != hp) || (rp != hp)) {
  504.  
  505.             /* did we scroll downward? */
  506.             if (lp == wp->w_dotp) {
  507.                 i = nlines - 1;
  508.                 break;
  509.             }
  510.  
  511.             /* did we scroll upward? */
  512.             if (rp == wp->w_dotp) {
  513.                 i = 0;
  514.                 break;
  515.             }
  516.  
  517.             /* advance forward and back */
  518.             if (lp != hp)
  519.                 lp = lforw(lp);
  520.             if (rp != hp)
  521.                 rp = lback(rp);
  522.  
  523.             /* problems????? */
  524.             if (lp == tp || rp == tp) {
  525.                 mlforce("BUG IN SMOOTH SCROLL--GET DAN!\n");
  526.                 TTgetc();
  527.             }
  528.         }
  529.     /* how far back to reframe? */
  530.     } else if (i > 0) {    /* only one screen worth of lines max */
  531.         if (--i >= nlines)
  532.             i = nlines - 1;
  533.     } else if (i < 0) {    /* negative update???? */
  534.         i += nlines;
  535.         if (i < 0)
  536.             i = 0;
  537.     } else
  538.         i = nlines / 2;
  539.  
  540.     /* backup to new line at top of window */
  541.     lp = wp->w_dotp;
  542.     while (i != 0 && lback(lp) != wp->w_bufp->b_linep) {
  543.         --i;
  544.         if (i < 0) {
  545.             mlforce("OTHER BUG IN DISPLAY --- GET DAN!!!\n");
  546.             TTgetc();
  547.         }
  548.         lp = lback(lp);
  549.     }
  550.  
  551.     /* and reset the current line at top of window */
  552.     wp->w_linep = lp;
  553.     wp->w_flag |= WFHARD;
  554.     wp->w_flag &= ~WFFORCE;
  555.     return(TRUE);
  556. }
  557.  
  558. /*    updone: update the current line to the virtual screen        */
  559.  
  560. PASCAL NEAR updone(wp)
  561.  
  562. WINDOW *wp;    /* window to update current line in */
  563.  
  564. {
  565.     register LINE *lp;    /* line to update */
  566.     register int sline;    /* physical screen line to update */
  567.     register int i;
  568.  
  569.     /* search down the line we want */
  570.     lp = wp->w_linep;
  571.     sline = wp->w_toprow;
  572.     while (lp != wp->w_dotp) {
  573.         ++sline;
  574.         lp = lforw(lp);
  575.     }
  576.  
  577.     /* and update the virtual line */
  578.     vscreen[sline]->v_flag |= VFCHG;
  579.     vscreen[sline]->v_flag &= ~VFREQ;
  580.     taboff = wp->w_fcol;
  581.     vtmove(sline, -taboff);
  582.     for (i=0; i < llength(lp); ++i)
  583.         vtputc(lgetc(lp, i));
  584. #if    COLOR
  585.     vscreen[sline]->v_rfcolor = wp->w_fcolor;
  586.     vscreen[sline]->v_rbcolor = wp->w_bcolor;
  587. #endif
  588.     vteeol();
  589.     taboff = 0;
  590. }
  591.  
  592. /*    updall: update all the lines in a window on the virtual screen */
  593.  
  594. PASCAL NEAR updall(wp)
  595.  
  596. WINDOW *wp;    /* window to update lines in */
  597.  
  598. {
  599.     register LINE *lp;    /* line to update */
  600.     register int sline;    /* physical screen line to update */
  601.     register int i;
  602.     register int nlines;    /* number of lines in the current window */
  603.  
  604.     /* search down the lines, updating them */
  605.     lp = wp->w_linep;
  606.     sline = wp->w_toprow;
  607.     nlines = wp->w_ntrows;
  608.     if (modeflag == FALSE)
  609.         nlines++;
  610.     taboff = wp->w_fcol;
  611.     while (sline < wp->w_toprow + nlines) {
  612.  
  613.         /* and update the virtual line */
  614.         vscreen[sline]->v_flag |= VFCHG;
  615.         vscreen[sline]->v_flag &= ~VFREQ;
  616.         vtmove(sline, -taboff);
  617.         if (lp != wp->w_bufp->b_linep) {
  618.             /* if we are not at the end */
  619.             for (i=0; i < llength(lp); ++i)
  620.                 vtputc(lgetc(lp, i));
  621.             lp = lforw(lp);
  622.         }
  623.  
  624.         /* make sure we are on screen */
  625.         if (vtcol < 0)
  626.             vtcol = 0;
  627.  
  628.         /* on to the next one */
  629. #if    COLOR
  630.         vscreen[sline]->v_rfcolor = wp->w_fcolor;
  631.         vscreen[sline]->v_rbcolor = wp->w_bcolor;
  632. #endif
  633.         vteeol();
  634.         ++sline;
  635.     }
  636.     taboff = 0;
  637. }
  638.  
  639. /*    updpos: update the position of the hardware cursor and handle extended
  640.         lines. This is the only update for simple moves.    */
  641.  
  642. PASCAL NEAR updpos()
  643.  
  644. {
  645.     register LINE *lp;
  646.     register int c;
  647.     register int i;
  648.  
  649.     /* find the current row */
  650.     lp = curwp->w_linep;
  651.     currow = curwp->w_toprow;
  652.     while (lp != curwp->w_dotp) {
  653.         ++currow;
  654.         lp = lforw(lp);
  655.     }
  656.  
  657.     /* find the current column */
  658.     curcol = 0;
  659.     i = 0;
  660.     while (i < curwp->w_doto) {
  661.         c = lgetc(lp, i++);
  662.         if (c == '\t')
  663.             curcol += - (curcol % tabsize) + (tabsize - 1);
  664.         else {
  665.             if (disphigh && c > 0x7f) {
  666.                 curcol += 2;
  667.                 c -= 0x80;
  668.             }
  669.             if (c < 0x20 || c == 0x7f)
  670.                 ++curcol;
  671.         }
  672.         ++curcol;
  673.     }
  674.  
  675.     /* adjust by the current first column position */
  676.     curcol -= curwp->w_fcol;
  677.  
  678.     /* make sure it is not off the left side of the screen */
  679.     while (curcol < 0) {
  680.         if (curwp->w_fcol >= hjump) {
  681.             curcol += hjump;
  682.             curwp->w_fcol -= hjump;
  683.         } else {
  684.             curcol += curwp->w_fcol;
  685.             curwp->w_fcol = 0;
  686.         }
  687.         curwp->w_flag |= WFHARD | WFMODE;
  688.     }
  689.  
  690.     /* if horizontall scrolling is enabled, shift if needed */
  691.     if (hscroll) {
  692.         while (curcol >= term.t_ncol - 1) {
  693.             curcol -= hjump;
  694.             curwp->w_fcol += hjump;
  695.             curwp->w_flag |= WFHARD | WFMODE;
  696.         }
  697.     } else {
  698.     /* if extended, flag so and update the virtual line image */
  699.         if (curcol >=  term.t_ncol - 1) {
  700.             vscreen[currow]->v_flag |= (VFEXT | VFCHG);
  701.             updext();
  702.         } else
  703.             lbound = 0;
  704.     }
  705.  
  706.     /* update the current window if we have to move it around */
  707.     if (curwp->w_flag & WFHARD)
  708.         updall(curwp);
  709.     if (curwp->w_flag & WFMODE)
  710.         modeline(curwp);
  711.     curwp->w_flag = 0;
  712. }
  713.  
  714. /*    upddex: de-extend any line that derserves it        */
  715.  
  716. PASCAL NEAR upddex()
  717.  
  718. {
  719.     register WINDOW *wp;
  720.     register LINE *lp;
  721.     register int i,j;
  722.     register int nlines;    /* number of lines in the current window */
  723.  
  724.     wp = wheadp;
  725.  
  726.     while (wp != NULL) {
  727.         lp = wp->w_linep;
  728.         i = wp->w_toprow;
  729.         nlines = wp->w_ntrows;
  730.         if (modeflag == FALSE)
  731.             nlines++;
  732.  
  733.         while (i < wp->w_toprow + nlines) {
  734.             if (vscreen[i]->v_flag & VFEXT) {
  735.                 if ((wp != curwp) || (lp != wp->w_dotp) ||
  736.                    (curcol < term.t_ncol - 1)) {
  737.                     taboff = wp->w_fcol;
  738.                     vtmove(i, -taboff);
  739.                     for (j = 0; j < llength(lp); ++j)
  740.                         vtputc(lgetc(lp, j));
  741.                     vteeol();
  742.                     taboff = 0;
  743.  
  744.                     /* this line no longer is extended */
  745.                     vscreen[i]->v_flag &= ~VFEXT;
  746.                     vscreen[i]->v_flag |= VFCHG;
  747.                 }
  748.             }
  749.             lp = lforw(lp);
  750.             ++i;
  751.         }
  752.         /* and onward to the next window */
  753.         wp = wp->w_wndp;
  754.     }
  755. }
  756.  
  757. /*    updgar: if the screen is garbage, clear the physical screen and
  758.         the virtual screen and force a full update        */
  759.  
  760. PASCAL NEAR updgar()
  761.  
  762. {
  763.     register int i;
  764. #if    MEMMAP == 0
  765.     register int j;
  766.     register char *txt;
  767. #endif
  768.  
  769.     for (i = 0; i < term.t_nrow; ++i) {
  770.         vscreen[i]->v_flag |= VFCHG;
  771. #if    REVSTA
  772.         vscreen[i]->v_flag &= ~VFREV;
  773. #endif
  774. #if    COLOR
  775.         vscreen[i]->v_fcolor = gfcolor;
  776.         vscreen[i]->v_bcolor = gbcolor;
  777. #endif
  778. #if    MEMMAP == 0
  779.         txt = pscreen[i]->v_text;
  780.         for (j = 0; j < term.t_ncol; ++j)
  781.             txt[j] = ' ';
  782. #endif
  783.     }
  784.  
  785.     movecursor(0, 0);         /* Erase the screen. */
  786. #if     COLOR && WINDOW_MSWIN
  787.         TTforg (gfcolor);   /* inform the driver of the colors */
  788.         TTbacg (gbcolor);   /* to be used for erase to end of page */
  789. #endif
  790. #if     REVSTA && WINDOW_MSWIN
  791.         TTrev (FALSE);
  792. #endif
  793.     (*term.t_eeop)();
  794. #if     !WINDOW_MSWIN
  795.     sgarbf = FALSE;          /* Erase-page clears */
  796.     mpresf = FALSE;          /* the message area. */
  797. #endif
  798. #if    COLOR
  799.     mlerase();            /* needs to be cleared if colored */
  800. #if     WINDOW_MSWIN
  801.     mpresf = FALSE;
  802. #endif
  803. #endif
  804. }
  805.  
  806. #if     !WINDOW_MSWIN
  807. /*    for simple screen size changes (no window re-allocation involved)
  808.     do the following things
  809. */
  810.  
  811. PASCAL NEAR update_size()
  812.  
  813. {
  814.     /* if we need the size update */
  815.     if ((first_screen->s_roworg != term.t_roworg) |
  816.         (first_screen->s_colorg != term.t_colorg) |
  817.         (first_screen->s_nrow != term.t_nrow) |
  818.         (first_screen->s_ncol != term.t_ncol)) {
  819.  
  820.         /* reset the terminal drivers size concept */
  821.         term.t_roworg = first_screen->s_roworg;
  822.         term.t_colorg = first_screen->s_colorg;
  823.         term.t_nrow = first_screen->s_nrow;
  824.         term.t_ncol = first_screen->s_ncol;
  825.  
  826.         /* make sure the update routines know we need a full update */
  827.         sgarbf = TRUE;
  828.     }
  829. }
  830. #endif
  831.  
  832. /*    Display a pop up window.  Page it for the user.  Any key other
  833.     than a space gets pushed back into the input stream to be interpeted
  834.     later as a command.
  835. */
  836.  
  837. #if    PROTO
  838. int PASCAL NEAR pop(BUFFER *popbuf)
  839. #else
  840. int PASCAL NEAR pop(popbuf)
  841.  
  842. BUFFER *popbuf;
  843. #endif
  844. {
  845.     register int index;    /* index into the current output line */
  846.     register int llen;    /* length of the current output line */
  847.     register int cline;    /* current screen line number */
  848.     LINE *lp;    /* ptr to next line to display */
  849.     int numlines;    /* remaining number of lines to display */
  850.     int c;        /* input character */
  851.  
  852.     /* add the barrior line to the end of the pop up buffer */
  853.     addline(popbuf, "------------------------------------------");
  854.  
  855.     /* set up to scan pop up buffer */
  856.     lp = lforw(popbuf->b_linep);
  857. /*    mlerase();    do we really need this? */
  858.     numlines = term.t_nrow-2;
  859.     cline = 0;
  860.  
  861.     while (lp != popbuf->b_linep) {
  862.  
  863.         /* update the virtual screen image for this one line */
  864.         vtmove(cline, 0);
  865.         llen = llength(lp);
  866.         for (index = 0; index < llen; index++)
  867.             vtputc(lp->l_text[index]);
  868.         vteeol();
  869. #if    COLOR
  870.         vscreen[cline]->v_rfcolor = gfcolor;
  871.         vscreen[cline]->v_rbcolor = gbcolor;
  872. #endif
  873.         vscreen[cline]->v_flag &= ~VFREQ;
  874.         vscreen[cline++]->v_flag |= VFCHG|VFCOL;
  875.  
  876.         if (numlines-- < 1) {
  877.  
  878.             /* update the virtual screen to the physical screen */
  879.             updupd(FALSE);
  880.  
  881.             /* tell the user there is more */
  882.             mlwrite("--- more ---");
  883.             TTflush();
  884.  
  885.             /* and see if they want more */
  886.             if ((c = tgetc()) != ' ') {
  887.                 cpending = TRUE;
  888.                 charpending = c;
  889.                 upwind();
  890.                 return(TRUE);
  891.             }
  892.  
  893.             /* reset the line counters */
  894.             numlines = term.t_nrow-2;
  895.             cline = 0;
  896.         }
  897.  
  898.         /* on to the next line */
  899.         lp = lforw(lp);
  900.     }
  901.     if (numlines > 0) {
  902.  
  903.         /* update the virtual screen to the physical screen */
  904.         updupd(FALSE);
  905.         TTflush();
  906.  
  907.         if ((c = tgetc()) != ' ') {
  908.             cpending = TRUE;
  909.             charpending = c;
  910.         }
  911.     }
  912.     upwind();
  913.     return(TRUE);
  914. }
  915.  
  916. /*    updupd: update the physical screen from the virtual screen    */
  917.  
  918. PASCAL NEAR updupd(force)
  919.  
  920. int force;    /* forced update flag */
  921.  
  922. {
  923.     register VIDEO *vp1;
  924.     register int i;
  925.  
  926.     for (i = 0; i < term.t_nrow; ++i) {
  927.         vp1 = vscreen[i];
  928.  
  929.         /* for each line that needs to be updated*/
  930.         if ((vp1->v_flag & VFCHG) != 0) {
  931. #if    TYPEAH
  932.             if (force == FALSE && typahead())
  933.                 return(TRUE);
  934. #endif
  935. #if    MEMMAP
  936.             updateline(i, vp1);
  937. #else
  938.             updateline(i, vp1, pscreen[i]);
  939. #endif
  940.         }
  941.     }
  942.     return(TRUE);
  943. }
  944.  
  945. /*    updext: update the extended line which the cursor is currently
  946.         on at a column greater than the terminal width. The line
  947.         will be scrolled right or left to let the user see where
  948.         the cursor is
  949.                                 */
  950. PASCAL NEAR updext()
  951.  
  952. {
  953.     register int rcursor;    /* real cursor location */
  954.     register LINE *lp;    /* pointer to current line */
  955.     register int j;     /* index into line */
  956.  
  957.     /* calculate what column the real cursor will end up in */
  958.     rcursor = ((curcol - term.t_ncol) % term.t_scrsiz)
  959.             + term.t_margin;
  960.     lbound = curcol - rcursor + 1;
  961.     taboff = lbound + curwp->w_fcol;
  962.  
  963.     /* scan through the line outputing characters to the virtual screen */
  964.     /* once we reach the left edge                    */
  965.     vtmove(currow, -taboff); /* start scanning offscreen */
  966.     lp = curwp->w_dotp;        /* line to output */
  967.     for (j=0; j<llength(lp); ++j)    /* until the end-of-line */
  968.         vtputc(lgetc(lp, j));
  969.  
  970.     /* truncate the virtual line, restore tab offset */
  971.     vteeol();
  972.     taboff = 0;
  973.  
  974.     /* and put a '$' in column 1 */
  975.     vscreen[currow]->v_text[0] = '$';
  976. }
  977.  
  978. /*
  979.  * Update a single line. This does not know how to use insert or delete
  980.  * character sequences; we are using VT52 functionality. Update the physical
  981.  * row and column variables. It does try an exploit erase to end of line.
  982.  */
  983. #if    MEMMAP
  984. /*    UPDATELINE specific code for memory mapped displays */
  985.  
  986. PASCAL NEAR updateline(row, vp)
  987.  
  988. int row;        /* row of screen to update */
  989. struct VIDEO *vp;    /* virtual screen image */
  990.  
  991. {
  992.     if (vp->v_flag & VFREQ)
  993.         TTrev(TRUE);
  994. #if    COLOR
  995.     scwrite(row, vp->v_text, vp->v_rfcolor, vp->v_rbcolor);
  996.     vp->v_fcolor = vp->v_rfcolor;
  997.     vp->v_bcolor = vp->v_rbcolor;
  998. #else
  999.     scwrite(row, vp->v_text, 7, 0);
  1000. #endif
  1001.     if (vp->v_flag & VFREQ)
  1002.         TTrev(FALSE);
  1003.     vp->v_flag &= ~(VFCHG | VFCOL);    /* flag this line as changed */
  1004.  
  1005. }
  1006.  
  1007. #else
  1008. PASCAL NEAR updateline(row, vp, pp)
  1009.  
  1010. int row;        /* row of screen to update */
  1011. struct VIDEO *vp;    /* virtual screen image */
  1012. struct VIDEO *pp;    /* physical screen image */
  1013.  
  1014. {
  1015.  
  1016.     register char *cp1;
  1017.     register char *cp2;
  1018.     register char *cp3;
  1019.     register char *cp4;
  1020.     register char *cp5;
  1021.     register int nbflag;    /* non-blanks to the right flag? */
  1022.     int rev;        /* reverse video flag */
  1023.     int req;        /* reverse video request flag */
  1024.     int upcol;        /* update column (KRS) */
  1025.  
  1026.     /* set up pointers to virtual and physical lines */
  1027.     cp1 = &vp->v_text[0];
  1028.     cp2 = &pp->v_text[0];
  1029.  
  1030. #if    COLOR
  1031.     TTforg(vp->v_rfcolor);
  1032.     TTbacg(vp->v_rbcolor);
  1033. #endif
  1034.  
  1035. #if    REVSTA | COLOR
  1036.     /* if we need to change the reverse video status of the
  1037.        current line, we need to re-write the entire line     */
  1038.     rev = (vp->v_flag & VFREV) == VFREV;
  1039.     req = (vp->v_flag & VFREQ) == VFREQ;
  1040.     if ((rev != req)
  1041. #if    COLOR
  1042.         || (vp->v_fcolor != vp->v_rfcolor) || (vp->v_bcolor != vp->v_rbcolor)
  1043. #endif
  1044. #if    HP150
  1045.     /* the HP150 has some reverse video problems */
  1046.         || req || rev
  1047. #endif
  1048.             ) {
  1049.         movecursor(row, 0);    /* Go to start of line. */
  1050.         /* set rev video if needed */
  1051.         if (rev != req)
  1052.             (*term.t_rev)(req);
  1053.  
  1054.         /* scan through the line and dump it to the screen and
  1055.            the virtual screen array                */
  1056.         cp3 = &vp->v_text[term.t_ncol];
  1057.         while (cp1 < cp3) {
  1058.             TTputc(*cp1);
  1059.             ++ttcol;
  1060.             *cp2++ = *cp1++;
  1061.         }
  1062.         /* turn rev video off */
  1063.         if (rev != req)
  1064.             (*term.t_rev)(FALSE);
  1065.  
  1066.         /* update the needed flags */
  1067.         vp->v_flag &= ~VFCHG;
  1068.         if (req)
  1069.             vp->v_flag |= VFREV;
  1070.         else
  1071.             vp->v_flag &= ~VFREV;
  1072. #if    COLOR
  1073.         vp->v_fcolor = vp->v_rfcolor;
  1074.         vp->v_bcolor = vp->v_rbcolor;
  1075. #endif
  1076.         pp->v_flag &= ~VFNEW;
  1077.         return(TRUE);
  1078.     }
  1079. #endif
  1080.  
  1081.     upcol = 0;
  1082.  
  1083.     if (!(pp->v_flag & VFNEW)) {
  1084.     /* advance past any common chars at the left */
  1085.     while (cp1 != &vp->v_text[term.t_ncol] && cp1[0] == cp2[0]) {
  1086.         ++cp1;
  1087.         ++upcol;
  1088.         ++cp2;
  1089.     }
  1090.  
  1091. #if    DBCS
  1092.     /* don't optimize on the left in the middle of a 2 byte char */
  1093.     if ((cp1 > &vp->v_text[0]) && is2byte(vp->v_text, cp1 - 1)) {
  1094.         --cp1;
  1095.         --upcol;
  1096.         --cp2;
  1097.     }
  1098. #endif
  1099.  
  1100.  
  1101. /* This can still happen, even though we only call this routine on changed
  1102.  * lines. A hard update is always done when a line splits, a massive
  1103.  * change is done, or a buffer is displayed twice. This optimizes out most
  1104.  * of the excess updating. A lot of computes are used, but these tend to
  1105.  * be hard operations that do a lot of update, so I don't really care.
  1106.  */
  1107.     /* if both lines are the same, no update needs to be done */
  1108.     if (cp1 == &vp->v_text[term.t_ncol]) {
  1109.         vp->v_flag &= ~VFCHG;        /* flag this line is changed */
  1110.         return(TRUE);
  1111.     }
  1112.     }
  1113.     /* find out if there is a match on the right */
  1114.     nbflag = FALSE;
  1115.     cp3 = &vp->v_text[term.t_ncol];
  1116.     cp4 = &pp->v_text[term.t_ncol];
  1117.  
  1118.     if (!(pp->v_flag & VFNEW)) {
  1119.     while (cp3[-1] == cp4[-1]) {
  1120.         --cp3;
  1121.         --cp4;
  1122.         if (cp3[0] != ' ')        /* Note if any nonblank */
  1123.             nbflag = TRUE;        /* in right match. */
  1124.     }
  1125.  
  1126. #if    DBCS
  1127.     /* don't stop in the middle of a 2 byte char */
  1128.     if (is2byte(vp->v_text, cp3-1) || is2byte(pp->v_text, cp4-1)) {
  1129.         ++cp3;
  1130.         ++cp4;
  1131.     }
  1132. #endif
  1133.     }
  1134.     cp5 = cp3;
  1135.  
  1136.     /* Erase to EOL ? */
  1137.     if (nbflag == FALSE && eolexist == TRUE && (req != TRUE)) {
  1138.         while (cp5!=cp1 && cp5[-1]==' ')
  1139.             --cp5;
  1140.  
  1141.         if (cp3-cp5 <= 3)        /* Use only if erase is */
  1142.             cp5 = cp3;        /* fewer characters. */
  1143.     }
  1144.  
  1145.     /* move to the begining of the text to update */
  1146.     movecursor(row, upcol);
  1147.  
  1148. #if    REVSTA
  1149.     if (rev)
  1150.         TTrev(TRUE);
  1151. #endif
  1152.  
  1153.     while (cp1 != cp5) {        /* Ordinary. */
  1154.         TTputc(*cp1);
  1155.         ++ttcol;
  1156.         *cp2++ = *cp1++;
  1157.     }
  1158.  
  1159.     if (cp5 != cp3) {        /* Erase. */
  1160.         TTeeol();
  1161.         while (cp1 != cp3)
  1162.             *cp2++ = *cp1++;
  1163.     }
  1164. #if    REVSTA
  1165.     if (rev)
  1166.         TTrev(FALSE);
  1167. #endif
  1168.     vp->v_flag &= ~VFCHG;       /* flag this line as updated */
  1169.     pp->v_flag &= ~VFNEW;
  1170.     return(TRUE);
  1171. }
  1172. #endif
  1173.  
  1174. /*
  1175.  * Redisplay the mode line for the window pointed to by the "wp". This is the
  1176.  * only routine that has any idea of how the modeline is formatted. You can
  1177.  * change the modeline format by hacking at this routine. Called by "update"
  1178.  * any time there is a dirty window.
  1179.  */
  1180. PASCAL NEAR modeline(wp)
  1181.  
  1182. WINDOW *wp;    /* window to update modeline for */
  1183.  
  1184. {
  1185.     register char *cp;
  1186.     register int c;
  1187.     register int n;        /* cursor position count */
  1188.     register BUFFER *bp;
  1189.     register int i;        /* loop index */
  1190.     register int lchar;     /* character to draw line in buffer with */
  1191.     register int firstm;    /* is this the first mode? */
  1192.     char tline[NLINE];    /* buffer for part of mode line */
  1193.     char time[6];        /* to hold current time */
  1194.  
  1195.     /* don't bother if there is none! */
  1196.     if (modeflag == FALSE)
  1197.         return;
  1198.  
  1199.     n = wp->w_toprow+wp->w_ntrows;        /* Location. */
  1200.  
  1201. /*
  1202.     Note that we assume that setting REVERSE will cause the terminal
  1203.     driver to draw with the inverted relationship of fcolor and
  1204.     bcolor, so that when we say to set the foreground color to "white"
  1205.     and background color to "black", the fact that "reverse" is
  1206.     enabled means that the terminal driver actually draws "black" on a
  1207.     background of "white".  Makes sense, no?  This way, devices for
  1208.     which the color controls are optional will still get the "reverse"
  1209.     signals.
  1210. */
  1211.  
  1212.     vscreen[n]->v_flag |= VFCHG | VFREQ | VFCOL;    /* Redraw next time. */
  1213. #if    COLOR
  1214.     vscreen[n]->v_rfcolor = 7;            /* black on */
  1215.     vscreen[n]->v_rbcolor = 0;            /* white.....*/
  1216. #endif
  1217.     vtmove(n, 0);                /* Seek to right line. */
  1218.     if (wp == curwp)            /* mark the current buffer */
  1219.         lchar = '=';
  1220.     else
  1221. #if    REVSTA
  1222.     if (revexist)
  1223.         lchar = ' ';
  1224.     else
  1225. #endif
  1226.         lchar = '-';
  1227.  
  1228.     bp = wp->w_bufp;
  1229.     if ((bp->b_flag&BFTRUNC) != 0)        /* "#" if truncated */
  1230.         vtputc('#');
  1231.     else
  1232.         vtputc(lchar);
  1233.  
  1234.     if ((bp->b_flag&BFCHG) != 0)        /* "*" if changed. */
  1235.         vtputc('*');
  1236.     else
  1237.         vtputc(lchar);
  1238.  
  1239.     if ((bp->b_flag&BFNAROW) != 0) {        /* "<>" if narrowed */
  1240.         vtputc('<');
  1241.         vtputc('>');
  1242.     } else {
  1243.         vtputc(lchar);
  1244.         vtputc(lchar);
  1245.     }
  1246.  
  1247.     n  = 4;
  1248.     strcpy(tline, " ");             /* Buffer name. */
  1249. #if     !WINDOW_MSWIN       /* we don't need that text, thanks to the
  1250.                    about box */
  1251.     strcat(tline, PROGNAME);
  1252.     strcat(tline, " ");
  1253.     strcat(tline, VERSION);
  1254.     strcat(tline, " ");
  1255. #endif
  1256.  
  1257.     /* display the time on the bottom most modeline if active */
  1258.     if (timeflag && wp->w_wndp == (WINDOW *)NULL) {
  1259.  
  1260.         /* get the current time/date string */
  1261.         getdtime(time);
  1262.         if (strcmp(time, "") != 0) {
  1263.  
  1264.             /* append the hour/min string */
  1265.             strcat(tline, "[");
  1266.             strcat(tline, time);
  1267.             strcat(tline, "] ");
  1268.             strcpy(lasttime, time);
  1269.         }
  1270.     }
  1271.  
  1272.     /* are we horizontally scrolled? */
  1273.     if (wp->w_fcol > 0) {
  1274.         strcat(tline, "[<");
  1275.         strcat(tline, int_asc(wp->w_fcol));
  1276.         strcat(tline, "]");
  1277.     }
  1278.  
  1279.     /* display the point position in buffer if on current modeline */
  1280.     if (posflag && wp == curwp) {
  1281.  
  1282.          strcat(tline, "L:");
  1283.          strcat(tline, int_asc(getlinenum(bp, wp->w_dotp)));
  1284.          strcat(tline, " C:");
  1285.          strcat(tline, int_asc(getccol(FALSE)));
  1286.          strcat(tline, " ");
  1287.     }
  1288.  
  1289.     /* display the modes */
  1290.     strcat(tline, "(");
  1291.     firstm = TRUE;
  1292.     for (i = 0; i < NUMMODES; i++)    /* add in the mode flags */
  1293.         if (wp->w_bufp->b_mode & (1 << i)) {
  1294.             if (firstm != TRUE)
  1295.                 strcat(tline, " ");
  1296.             firstm = FALSE;
  1297.             strcat(tline, modename[i]);
  1298.         }
  1299.     strcat(tline,") ");
  1300.  
  1301.     cp = &tline[0];
  1302.     while ((c = *cp++) != 0) {
  1303.         vtputc(c);
  1304.         ++n;
  1305.     }
  1306.  
  1307. #if    0
  1308.     vtputc(lchar);
  1309.     vtputc((wp->w_flag&WFCOLR) != 0  ? 'C' : lchar);
  1310.     vtputc((wp->w_flag&WFMODE) != 0  ? 'M' : lchar);
  1311.     vtputc((wp->w_flag&WFHARD) != 0  ? 'H' : lchar);
  1312.     vtputc((wp->w_flag&WFEDIT) != 0  ? 'E' : lchar);
  1313.     vtputc((wp->w_flag&WFMOVE) != 0  ? 'V' : lchar);
  1314.     vtputc((wp->w_flag&WFFORCE) != 0 ? 'F' : lchar);
  1315.     vtputc(lchar);
  1316.     n += 8;
  1317. #endif
  1318.  
  1319.     vtputc(lchar);
  1320.     vtputc(lchar);
  1321.     vtputc(' ');
  1322.     n += 3;
  1323.     cp = &bp->b_bname[0];
  1324.  
  1325.     while ((c = *cp++) != 0) {
  1326.         vtputc(c);
  1327.         ++n;
  1328.     }
  1329.  
  1330.     vtputc(' ');
  1331.     vtputc(lchar);
  1332.     vtputc(lchar);
  1333.     n += 3;
  1334.  
  1335.     if (bp->b_fname[0] != 0) {    /* File name. */
  1336.         vtputc(' ');
  1337.         ++n;
  1338.         cp = TEXT34;
  1339. /*                   "File: " */
  1340.  
  1341.         while ((c = *cp++) != 0) {
  1342.             vtputc(c);
  1343.             ++n;
  1344.         }
  1345.  
  1346.         cp = &bp->b_fname[0];
  1347.  
  1348.         while ((c = *cp++) != 0) {
  1349.             vtputc(c);
  1350.             ++n;
  1351.             }
  1352.  
  1353.         vtputc(' ');
  1354.         ++n;
  1355.     }
  1356.  
  1357.     while (n < term.t_ncol) {    /* Pad to full width. */
  1358.         vtputc(lchar);
  1359.         ++n;
  1360.     }
  1361. }
  1362.  
  1363. VOID PASCAL NEAR getdtime(ts)    /* get the current display time string */
  1364.  
  1365. char *ts;
  1366.  
  1367. {
  1368.     char buf[80];
  1369.  
  1370.     strcpy(buf, timeset());
  1371.     if (strcmp(buf, errorm) == 0) {
  1372.         *ts = 0;
  1373.         return;
  1374.     }
  1375.  
  1376.     buf[16] = 0;
  1377.     strcpy(ts, &buf[11]);
  1378.     return;
  1379. }
  1380.  
  1381. VOID PASCAL NEAR upmode()    /* update all the mode lines */
  1382.  
  1383. {
  1384.     register WINDOW *wp;
  1385. #if     WINDOW_MSWIN
  1386.     SCREEN  *sp;
  1387.  
  1388.     /* loop through all screens to update even partially hidden ones.
  1389.        Note that we process the "first" screen last */
  1390.     sp = first_screen;
  1391.     do {
  1392.         sp = sp->s_next_screen;
  1393.         if (sp == (SCREEN *)NULL) sp = first_screen;
  1394.         vtscreen (sp);
  1395.         wheadp = sp->s_first_window;
  1396. #endif
  1397.     wp = wheadp;
  1398.     while (wp != NULL) {
  1399.         wp->w_flag |= WFMODE;
  1400.         wp = wp->w_wndp;
  1401.     }
  1402. #if     WINDOW_MSWIN
  1403.     } while (sp != first_screen);
  1404. #endif
  1405. }
  1406.  
  1407. VOID PASCAL NEAR upwind()    /* force hard updates on all windows */
  1408.  
  1409. {
  1410.     register WINDOW *wp;
  1411. #if     WINDOW_MSWIN
  1412.     SCREEN  *sp;
  1413.  
  1414.     /* loop through all screens to update even partially hidden ones.
  1415.        Note that we process the "first" screen last */
  1416.     sp = first_screen;
  1417.     do {
  1418.         sp = sp->s_next_screen;
  1419.         if (sp == (SCREEN *)NULL) sp = first_screen;
  1420.         vtscreen (sp);
  1421.         wheadp = sp->s_first_window;
  1422. #endif
  1423.     wp = wheadp;
  1424.     while (wp != NULL) {
  1425.         wp->w_flag |= WFHARD|WFMODE;
  1426.         wp = wp->w_wndp;
  1427.     }
  1428. #if     WINDOW_MSWIN
  1429.     } while (sp != first_screen);
  1430. #endif
  1431. }
  1432.  
  1433. /*
  1434.  * Send a command to the terminal to move the hardware cursor to row "row"
  1435.  * and column "col". The row and column arguments are origin 0. Optimize out
  1436.  * random calls. Update "ttrow" and "ttcol".
  1437.  */
  1438. PASCAL NEAR movecursor(row, col)
  1439.  
  1440. int row, col;
  1441.  
  1442. {
  1443. #if     WINDOW_MSWIN
  1444.         if (row >= term.t_nrow) row = term.t_mrow;
  1445.             /* emphasize move into message line to avoid confusion with
  1446.            another, larger, screen */
  1447.         if (defferupdate) update (TRUE);
  1448.             /* a pending update could move the cursor somewhere else, so
  1449.            we make sure it can't happen */
  1450.     if (row!=ttrow || col!=ttcol || foulcursor) {
  1451. /* in MSWIN, the message line is a separate entity and a call to
  1452.    vtscreen after a movecursor calls might actually have "stolen" the
  1453.    cursor away from the message line! */
  1454.             foulcursor = FALSE;
  1455. #else
  1456.     if (row!=ttrow || col!=ttcol) {
  1457. #endif
  1458.         ttrow = row;
  1459.         ttcol = col;
  1460.         TTmove(row, col);
  1461.     }
  1462. }
  1463.  
  1464. /*
  1465.  * Erase the message line. This is a special routine because the message line
  1466.  * is not considered to be part of the virtual screen. It always works
  1467.  * immediately; the terminal buffer is flushed via a call to the flusher.
  1468.  */
  1469.  
  1470. VOID PASCAL NEAR mlferase()
  1471.  
  1472. {
  1473.     register int save_discmd;
  1474.  
  1475.     save_discmd = discmd;
  1476.     discmd = TRUE;
  1477.     mlerase();
  1478.     discmd = save_discmd;;
  1479. }
  1480.  
  1481. VOID PASCAL NEAR mlerase()
  1482.  
  1483. {
  1484.     int i;
  1485.     
  1486.     movecursor(term.t_nrow, 0);
  1487.     if (discmd == FALSE)
  1488.         return;
  1489.  
  1490. #if    COLOR
  1491.     TTforg(7);
  1492.     TTbacg(gbcolor);
  1493. #endif
  1494.  
  1495.     if (eolexist == TRUE)
  1496.         TTeeol();
  1497.     else {
  1498.         for (i = 0; i < term.t_ncol - 1; i++)
  1499.             TTputc(' ');
  1500.  
  1501.         /* force the move! */
  1502. /*        movecursor(term.t_nrow, 1);*/
  1503.         movecursor(term.t_nrow, 0);
  1504.     }
  1505.     TTflush();
  1506.     mpresf = FALSE;
  1507. }
  1508.  
  1509. /*
  1510.  * Write a message into the message line. Keep track of the physical cursor
  1511.  * position. A small class of printf like format items is handled. Assumes the
  1512.  * stack grows down; this assumption is made by the "+=" in the argument scan
  1513.  * loop. If  STACK_GROWS_UP  is set in estruct.h, then we'll assume that the
  1514.  * stack grows up and use "-=" instead of "+=". Set the "message line"
  1515.  *  flag TRUE.  Don't write beyond the end of the current terminal width.
  1516.  */
  1517.  
  1518. PASCAL NEAR mlout(c)
  1519.  
  1520. int c;    /* character to write */
  1521.  
  1522. {
  1523. #if WINDOW_MSWIN
  1524.         if (ttcol + 1 < NSTRING)
  1525. #else
  1526.     if (ttcol + 1 < term.t_ncol)
  1527. #endif
  1528.         TTputc(c);
  1529.     if (c != '\b')
  1530.         *lastptr++ = c;
  1531.     else if (lastptr > &lastmesg[0])
  1532.         --lastptr;
  1533. }
  1534.  
  1535. #if    VARARG
  1536. #if    VARG
  1537. CDECL NEAR mlwrite(va_alist)
  1538.  
  1539. va_dcl        /* variable argument list
  1540.             arg1 = format string
  1541.             arg2+ = arguments in that string */
  1542.  
  1543. {
  1544.     register int c;     /* current char in format string */
  1545.     register char *fmt;    /* ptr to format string */
  1546.     register va_list ap;    /* ptr to current data field */
  1547.     int arg_int;        /* integer argument */
  1548.     long arg_long;        /* long argument */
  1549.     char *arg_str;        /* string argument */
  1550.  
  1551.     /* if we are not currently echoing on the command line, abort this */
  1552.     if (discmd == FALSE)
  1553.         return;
  1554.  
  1555. #if    COLOR
  1556.     /* set up the proper colors for the command line */
  1557.     TTforg(7);
  1558.     TTbacg(gbcolor);
  1559. #endif
  1560.  
  1561.     /* point to the first argument */
  1562.     va_start(ap);
  1563.     fmt = va_arg(ap, char *);
  1564.  
  1565.     /* if we can not erase to end-of-line, do it manually */
  1566.     if (eolexist == FALSE) {
  1567.         mlerase();
  1568.         TTflush();
  1569.     }
  1570.  
  1571.     movecursor(term.t_nrow, 0);
  1572.      lastptr = &lastmesg[0];        /* setup to record message */
  1573.     while ((c = *fmt++) != 0) {
  1574.         if (c != '%') {
  1575.             mlout(c);
  1576.             ++ttcol;
  1577.         } else {
  1578.             c = *fmt++;
  1579.             switch (c) {
  1580.                 case 'd':
  1581.                     arg_int = va_arg(ap, int);
  1582.                     mlputi(arg_int, 10);
  1583.                     break;
  1584.  
  1585.                 case 'o':
  1586.                     arg_int = va_arg(ap, int);
  1587.                     mlputi(arg_int, 8);
  1588.                     break;
  1589.  
  1590.                 case 'x':
  1591.                     arg_int = va_arg(ap, int);
  1592.                     mlputi(arg_int, 16);
  1593.                     break;
  1594.  
  1595.                 case 'D':
  1596.                     arg_long = va_arg(ap, long);
  1597.                     mlputli(arg_long, 10);
  1598.                     break;
  1599.  
  1600.                 case 's':
  1601.                     arg_str = va_arg(ap, char *);
  1602.                     mlputs(arg_str);
  1603.                     break;
  1604.  
  1605.                 case 'f':
  1606.                     arg_int = va_arg(ap, int);
  1607.                     mlputf(arg_int);
  1608.                     break;
  1609.  
  1610.                 default:
  1611.                     mlout(c);
  1612.                     ++ttcol;
  1613.             }
  1614.         }
  1615.     }
  1616.  
  1617.     /* if we can, erase to the end of screen */
  1618.     if (eolexist == TRUE)
  1619.         TTeeol();
  1620.     TTflush();
  1621.     mpresf = TRUE;
  1622.     *lastptr = 0;    /* terminate lastmesg[] */
  1623.     va_end(ap);
  1624. }
  1625. #else
  1626. CDECL NEAR mlwrite(char *fmt, ...)
  1627. /* char * fmt;*/
  1628.  
  1629.         /* variable argument list
  1630.             arg1 = format string
  1631.             arg2+ = arguments in that string */
  1632.  
  1633. {
  1634.     register int c;     /* current char in format string */
  1635.     va_list ap;        /* ptr to current data field */
  1636.     int arg_int;        /* integer argument */
  1637.     long arg_long;        /* long argument */
  1638.     char *arg_str;        /* string argument */
  1639.  
  1640.     /* if we are not currently echoing on the command line, abort this */
  1641.     if (discmd == FALSE)
  1642.         return;
  1643.  
  1644. #if    COLOR
  1645.     /* set up the proper colors for the command line */
  1646.     TTforg(7);
  1647.     TTbacg(gbcolor);
  1648. #endif
  1649.  
  1650.     /* point to the first argument */
  1651.     va_start(ap, fmt);
  1652.  
  1653.     /* if we can not erase to end-of-line, do it manually */
  1654.     if (eolexist == FALSE) {
  1655.         mlerase();
  1656.         TTflush();
  1657.     }
  1658.  
  1659.     movecursor(term.t_nrow, 0);
  1660.      lastptr = &lastmesg[0];        /* setup to record message */
  1661.     while ((c = *fmt++) != 0) {
  1662.         if (c != '%') {
  1663.             mlout(c);
  1664.             ++ttcol;
  1665.         } else {
  1666.             c = *fmt++;
  1667.             switch (c) {
  1668.                 case 'd':
  1669.                     arg_int = va_arg(ap, int);
  1670.                     mlputi(arg_int, 10);
  1671.                     break;
  1672.  
  1673.                 case 'o':
  1674.                     arg_int = va_arg(ap, int);
  1675.                     mlputi(arg_int, 8);
  1676.                     break;
  1677.  
  1678.                 case 'x':
  1679.                     arg_int = va_arg(ap, int);
  1680.                     mlputi(arg_int, 16);
  1681.                     break;
  1682.  
  1683.                 case 'D':
  1684.                     arg_long = va_arg(ap, long);
  1685.                     mlputli(arg_long, 10);
  1686.                     break;
  1687.  
  1688.                 case 's':
  1689.                     arg_str = va_arg(ap, char *);
  1690.                     mlputs(arg_str);
  1691.                     break;
  1692.  
  1693.                 case 'f':
  1694.                     arg_int = va_arg(ap, int);
  1695.                     mlputf(arg_int);
  1696.                     break;
  1697.  
  1698.                 default:
  1699.                     mlout(c);
  1700.                     ++ttcol;
  1701.             }
  1702.         }
  1703.     }
  1704.  
  1705.     /* if we can, erase to the end of screen */
  1706.     if (eolexist == TRUE)
  1707.         TTeeol();
  1708.     TTflush();
  1709.     mpresf = TRUE;
  1710.     *lastptr = 0;    /* terminate lastmesg[] */
  1711.     va_end(ap);
  1712. }
  1713. #endif
  1714. #else
  1715.  
  1716. #if    STACK_GROWS_UP
  1717. #define    ADJUST(ptr, dtype)    ptr -= sizeof(dtype)
  1718. #else
  1719. #define    ADJUST(ptr, dtype)    ptr += sizeof(dtype)
  1720. #endif
  1721.  
  1722. CDECL NEAR mlwrite(fmt)
  1723.  
  1724. char *fmt;    /* format string for output */
  1725.  
  1726. {
  1727.     register int c;     /* current char in format string */
  1728.     register char *ap;    /* ptr to current data field */
  1729.  
  1730.     /* if we are not currently echoing on the command line, abort this */
  1731.     if (discmd == FALSE)
  1732.         return;
  1733.  
  1734. #if    COLOR
  1735.     /* set up the proper colors for the command line */
  1736.     TTforg(7);
  1737.     TTbacg(gbcolor);
  1738. #endif
  1739.  
  1740.     /* point to the first argument */
  1741.     ap = &fmt;
  1742.     ADJUST(ap, char *);
  1743.  
  1744.     /* if we can not erase to end-of-line, do it manually */
  1745.     if (eolexist == FALSE) {
  1746.         mlerase();
  1747.         TTflush();
  1748.     }
  1749.  
  1750.     movecursor(term.t_nrow, 0);
  1751.      lastptr = &lastmesg[0];        /* setup to record message */
  1752.     while ((c = *fmt++) != 0) {
  1753.         if (c != '%') {
  1754.             mlout(c);
  1755.             ++ttcol;
  1756.         } else {
  1757.             c = *fmt++;
  1758.             switch (c) {
  1759.                 case 'd':
  1760.                     mlputi(*(int *)ap, 10);
  1761.                             ADJUST(ap, int);
  1762.                     break;
  1763.  
  1764.                 case 'o':
  1765.                     mlputi(*(int *)ap,  8);
  1766.                     ADJUST(ap, int);
  1767.                     break;
  1768.  
  1769.                 case 'x':
  1770.                     mlputi(*(int *)ap, 16);
  1771.                     ADJUST(ap, int);
  1772.                     break;
  1773.  
  1774.                 case 'D':
  1775.                     mlputli(*(long *)ap, 10);
  1776.                     ADJUST(ap, long);
  1777.                     break;
  1778.  
  1779.                 case 's':
  1780.                     mlputs(*(char **)ap);
  1781.                     ADJUST(ap, char *);
  1782.                     break;
  1783.  
  1784.                 case 'f':
  1785.                     mlputf(*(int *)ap);
  1786.                     ADJUST(ap, int);
  1787.                     break;
  1788.  
  1789.                 default:
  1790.                     mlout(c);
  1791.                     ++ttcol;
  1792.             }
  1793.         }
  1794.     }
  1795.  
  1796.     /* if we can, erase to the end of screen */
  1797.     if (eolexist == TRUE)
  1798.         TTeeol();
  1799.     TTflush();
  1800.     mpresf = TRUE;
  1801.     *lastptr = 0;    /* terminate lastmesg[] */
  1802. }
  1803. #endif
  1804.  
  1805. #if     !WINDOW_MSWIN
  1806. /* display a serious error message (like "out of memory"). This is
  1807.    replaced by a system-specific function when a multitasking system
  1808.    that does not like these kind of errors is used, so that the user can
  1809.    be offered to abort the application */
  1810.  
  1811. PASCAL NEAR mlabort(s)
  1812. char *s;
  1813. {
  1814.     mlforce(s);
  1815. }
  1816. #endif
  1817. /*    Force a string out to the message line regardless of the
  1818.     current $discmd setting. This is needed when $debug is TRUE
  1819.     and for the write-message and clear-message-line commands
  1820. */
  1821.  
  1822. PASCAL NEAR mlforce(s)
  1823.  
  1824. char *s;    /* string to force out */
  1825.  
  1826. {
  1827.     register int oldcmd;    /* original command display flag */
  1828.  
  1829.     oldcmd = discmd;    /* save the discmd value */
  1830.     discmd = TRUE;        /* and turn display on */
  1831.     mlwrite(s);        /* write the string out */
  1832.     discmd = oldcmd;    /* and restore the original setting */
  1833. }
  1834.  
  1835. /*
  1836.  * Write out a string. Update the physical cursor position. This assumes that
  1837.  * the characters in the string all have width "1"; if this is not the case
  1838.  * things will get screwed up a little.
  1839.  */
  1840.  
  1841. PASCAL NEAR mlputs(s)
  1842.  
  1843. char *s;
  1844.  
  1845. {
  1846.     register int c;
  1847.  
  1848.     while ((c = *s++) != 0) {
  1849.         mlout(c);
  1850.         ++ttcol;
  1851.     }
  1852. }
  1853.  
  1854. /*
  1855.  * Write out an integer, in the specified radix. Update the physical cursor
  1856.  * position.
  1857.  */
  1858. PASCAL NEAR mlputi(i, r)
  1859.  
  1860. int i, r;
  1861.  
  1862.     {
  1863.     register int q;
  1864.     static char hexdigits[] = "0123456789ABCDEF";
  1865.  
  1866.     if (i < 0)
  1867.     {
  1868.     i = -i;
  1869.     mlout('-');
  1870.     }
  1871.  
  1872.     q = i/r;
  1873.  
  1874.     if (q != 0)
  1875.     mlputi(q, r);
  1876.  
  1877.     mlout(hexdigits[i%r]);
  1878.     ++ttcol;
  1879.     }
  1880.  
  1881. /*
  1882.  * do the same except as a long integer.
  1883.  */
  1884. PASCAL NEAR mlputli(l, r)
  1885.  
  1886. long l;
  1887. int r;
  1888.  
  1889.     {
  1890.     register long q;
  1891.  
  1892.     if (l < 0)
  1893.     {
  1894.     l = -l;
  1895.     mlout('-');
  1896.     }
  1897.  
  1898.     q = l/r;
  1899.  
  1900.     if (q != 0)
  1901.     mlputli(q, r);
  1902.  
  1903.     mlout((int)(l%r)+'0');
  1904.     ++ttcol;
  1905.     }
  1906.  
  1907. /*
  1908.  *    write out a scaled integer with two decimal places
  1909.  */
  1910.  
  1911. PASCAL NEAR mlputf(s)
  1912.  
  1913. int s;    /* scaled integer to output */
  1914.  
  1915. {
  1916.     int i;    /* integer portion of number */
  1917.     int f;    /* fractional portion of number */
  1918.  
  1919.     /* break it up */
  1920.     i = s / 100;
  1921.     f = s % 100;
  1922.  
  1923.     /* send out the integer portion */
  1924.     mlputi(i, 10);
  1925.     mlout('.');
  1926.     mlout((f / 10) + '0');
  1927.     mlout((f % 10) + '0');
  1928.     ttcol += 3;
  1929. }       
  1930.  
  1931. #if RAINBOW
  1932.  
  1933. PASCAL NEAR putline(row, col, buf)
  1934.     int row, col;
  1935.     char buf[];
  1936.     {
  1937.     int n;
  1938.  
  1939.     n = strlen(buf);
  1940.     if (col + n - 1 > term.t_ncol)
  1941.     n = term.t_ncol - col + 1;
  1942.     Put_Data(row, col, n, buf);
  1943.     }
  1944. #endif
  1945.  
  1946.